package com.example.zqm.git72.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.Adapter.MasterFaqListAdapter;
import com.example.zqm.git72.Adapter.MasterVideoListAdapter;
import com.example.zqm.git72.JavaBean.CreateFaq;
import com.example.zqm.git72.JavaBean.Master;
import com.example.zqm.git72.JavaBean.Masterfaq;
import com.example.zqm.git72.Model.MasterModel;
import com.example.zqm.git72.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MasterFaqActivity extends AppCompatActivity {
    String master_id;
    String user_id;
    String question;
    private EditText editText;
    private Button back;
    RecyclerView recyclerView;
    public List<Masterfaq> masterfaq;
    private Button faq;
//    private EditText write;
//    private  Button send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_masterfaq);
        recyclerView=(RecyclerView) findViewById(R.id.masterfaq);
        recyclerView.setLayoutManager(new LinearLayoutManager(MasterFaqActivity.this));
        intNN();
        initView();
        faq =(Button)findViewById(R.id.faq) ;
        faq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText =new EditText(MasterFaqActivity.this);
                new AlertDialog.Builder(MasterFaqActivity.this)
                        .setTitle("请输入问题")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(editText)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                question = editText.getText().toString();
                                if(question.equals(""))
                                {
                                    Toast.makeText(MasterFaqActivity.this,"问题不能为空！",Toast.LENGTH_SHORT).show();
                                }else{
                                    createfaq();
                                }

                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();

            }
        });
    }
    public void initView(){
        back =(Button)findViewById(R.id.back);
//        write=(EditText)findViewById(R.id.wirte);
//        send=(Button)findViewById(R.id.send);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(MasterFaqActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

    }
    protected void intNN(){
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        master_id=bundle.getString("id");
       SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
       user_id=sharedPreferences.getString("user_id","");
        MasterModel masterModel =new MasterModel();
        Call<List<Masterfaq>> call= masterModel.getListfaq(master_id);
        call.enqueue(new Callback<List<Masterfaq>>() {
            @Override
            public void onResponse(Call<List<Masterfaq>> call, Response<List<Masterfaq>> response) {
                masterfaq =response.body();
                recyclerView.setAdapter(new MasterFaqListAdapter(MasterFaqActivity.this,masterfaq));
            }

            @Override
            public void onFailure(Call<List<Masterfaq>> call, Throwable t) {

            }
        });


    }
    public  void  createfaq(){
        question = editText.getText().toString();
        MasterModel masterModel =new MasterModel();
        Call<String> call =masterModel.createFaq(user_id,master_id,question);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Toast.makeText(MasterFaqActivity.this,"提问成功！",Toast.LENGTH_SHORT).show();
                refresh();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(MasterFaqActivity.this,"提问失败！",Toast.LENGTH_SHORT).show();
            }
        });

    }

  public  void refresh(){
      intNN();
  }

}
